window.addEvent('domready', function(){

	var gradeField = $('grade'), gradeCategory = $('grade_category');

	function validateGrade(e)
	{
		if(!isNumber(gradeField.value))
		{
			gradeCategory.setHTML("Please Enter a number in 'Grade' field(spaces are not allowed).");
		}
		else 
		{
			if(gradeField.value.toInt() >= 0 && gradeField.value.toInt() < 50)
			{
				gradeCategory.setHTML("F");
			}
			else if(gradeField.value.toInt() >= 50 && gradeField.value.toInt() < 65)
			{
				gradeCategory.setHTML("P");
			}
			else if(gradeField.value.toInt() >= 65 && gradeField.value.toInt() < 75)
			{
				gradeCategory.setHTML("C");
			}
			else if(gradeField.value.toInt() >= 75 && gradeField.value.toInt() < 85)
			{
				gradeCategory.setHTML("D");
			}
			else if(gradeField.value.toInt() >= 85 && gradeField.value.toInt() <= 100)
			{
				gradeCategory.setHTML("HD");
			}
			else
			{
				gradeCategory.setHTML("The range of grade is from 0 to 100.");
			}
		}
	}
	gradeField.addEvent('keyup',validateGrade.bindWithEvent(gradeField));
	gradeField.addEvent('focus',validateGrade.bindWithEvent(gradeField));

	function isNumber(str)
	{
		return /^[0-9]+$/.test(str);
	}
});
